import {
  Column,
  Entity,
  PrimaryGeneratedColumn,
  ManyToMany,
  JoinTable,
} from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { CommonEntity } from 'src/common/entity/index.entity';
import { RoleEntity } from '../../role/entities/role.entity';

export enum PermissionTypeEnum {
  LINK = 'link', // 超链接
  DIRECTORY = 'directory', // 目录
  MENU = 'menu', // 菜单
  BUTTON = 'button', // 按钮
}

@Entity('permission', {})
export class PermissionEntity extends CommonEntity {
  @ApiProperty({ default: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' })
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ApiProperty({ default: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' })
  @Column('uuid', { name: 'parent_id', nullable: true })
  parentId: string | null;

  @ApiProperty({ default: 'permission' })
  @Column('varchar', { name: 'title', length: 50, comment: '前端权限显示名称' })
  title: string;

  @ApiProperty({ default: PermissionTypeEnum.MENU })
  @Column('enum', {
    name: 'type',
    enum: PermissionTypeEnum,
    comment: '前端权限类型',
  })
  type: PermissionTypeEnum;

  @ApiProperty({ default: 'home' })
  @Column('varchar', {
    name: 'menu_name',
    length: 50,
    nullable: true,
    comment: '菜单名',
  })
  name: string | null;

  @ApiProperty({ default: 'Menu' })
  @Column('varchar', {
    name: 'menu_icon',
    length: 50,
    nullable: true,
    comment: '图标',
  })
  icon: string | null;

  @ApiProperty({ default: '/demo' })
  @Column('varchar', {
    name: 'path',
    nullable: true,
    comment: '菜单或链接路径',
  })
  path: string | null;

  @ApiProperty({ default: 'src/view/demo/index.vue' })
  @Column('varchar', {
    name: 'component',
    nullable: true,
    comment: '前端组件对应位置',
  })
  component: string | null;

  @ApiProperty({ type: RoleEntity, isArray: true })
  @ManyToMany(() => RoleEntity)
  @JoinTable({
    name: 'role_permission_relation',
    joinColumn: {
      name: 'permission_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'role_id',
      referencedColumnName: 'id',
    },
  })
  roles: RoleEntity[];
}
